package org.longsheng.GroovyLearn.controller

import groovy.util.logging.Slf4j
import org.apache.commons.text.StringEscapeUtils
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths;

@RestController
@RequestMapping("/test/")
@Slf4j
class TestController {

    @GetMapping("test1")
    public String test1() {
        log.info("文件路径");

        Path file = Paths.get("F:\\Study\\groovy-learn\\src\\main\\groovy\\org\\longsheng\\GroovyLearn\\controller\\TestController.groovy");
        Charset charset = Charset.forName("UTF-8");
        StringBuilder result = new StringBuilder();
        try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(StringEscapeUtils.escapeHtml4(line) + "<br>\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    @GetMapping("test2")
    public String test2() {
        // 动态类型
        def t1 = 't1'
        String t2 = 't2'
        def t3 = 't3'

        // 字符串
        def a = 'hello "world"' // 单引号字符串
        def b = "What's the weather like?" // 双引号字符串
        assert 'ab' == 'a' + 'b' // 用加号连接字符串，用等号对比字符串
        def aMultilineString = '''line one
        line two
        line three''' // 三个单引号字符串，支持直接换行

        // 斜线字符串中，反斜线不需要转义，常用于正则表达式
        def fooPattern = /.*foo.*/

        // 双引号字符串支持用$嵌入变量
        def name = 'Tom'
        def greeting = "Hello ${name}"

        // 如需函数调用，则$后表达式要加大括号
        def pi = 3.14
        def piString = "Pi = ${pi.toString()}"

        return piString
    }

    // 闭包
    def c1 = {
        println 'hello'
    }

    def c2 = {a, b ->
        println a
        println b
    }

    def c3 = { int a, String b ->
        println a
        println b
    }

    def c4 = { ->
        println 'hello'
    }

    def c5 = {
        println it
    }

    def c6 = {
        println it + 1
    }

    def c7 = {
        it + 1
    }

    @GetMapping("test3")
    public String test3() {
        c5.call('text1')
        c5('text2')

        return '闭包' + new Date()
    }

    // 方法/闭包的定义与调用
    def f1 = { text ->
        println text
    }

    def f2(text) {
        println text
    }

    void f3(String text) {
        println text
    }

    def func = { text, Closure closure ->
        println text
        closure.call()
    }

    @GetMapping("test4")
    public String test4() {
        func('1', {
            println '2'
        })

        func('3', {
            println '4'
        })

        func('5', {
            println '6'
        })

        return '方法/闭包的定义与调用'
    }

    class MyDelegate {
        def func = {
            println('hello')
        }
    }

    def c = {
        func()
    }

    @GetMapping("test5")
    public String test5() {
        c.delegate = new MyDelegate();
        c.call();
    }
}